/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.odhixon.volarme.gui;

import com.odhixon.volarme.facades.RegistrosFachada;
import com.odhixon.volarme.persistence.RegistroPerst;
import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;

/**
 * RegistrosForm.
 * @author Ariel Aguirre <odhixon@gmail.com>
 */
public class RegistrosForm extends Form implements ActionListener {

    static public RegistrosForm instance = null;

    static public RegistrosForm getInstance() {
        if (instance == null) {
            instance = new RegistrosForm();
        }
        return instance;
    }
    private List registrosList;
    private ObjectsModel registrosModel;
    private Command volverCommand;
    private Command nuevoCommand;

    public RegistrosForm() {
        super();
        //this form.
        this.setTitle("Registros de vuelo");
        this.setScrollable(false);
        //registros list.
        registrosModel = new ObjectsModel() {

            public void load() {
                objects = RegistrosFachada.getInstance().registrosList();
            }
        };
        registrosList = new List(registrosModel);
        registrosList.setListCellRenderer(new RegistrosRender());
        registrosList.addActionListener(this);
        this.addComponent(registrosList);
        volverCommand = new Command("Volver") {

            public void actionPerformed(ActionEvent actionEvent) {
                OpcionesForm.getInstance().show();
            }
        };
        this.addCommand(volverCommand);
        nuevoCommand = new Command("Nuevo") {

            public void actionPerformed(ActionEvent actionEvent) {
                RegistroForm registroForm = RegistroForm.getInstance();
                registroForm.setBackForm(RegistrosForm.getInstance());
                registroForm.setRegistro(null);
                registroForm.show();
            }
        };
        this.addCommand(nuevoCommand);
    }

    public void actionPerformed(ActionEvent actionEvent) {
        int index = registrosModel.getSelectedIndex();
        RegistroPerst registro = (RegistroPerst) registrosModel.getItemAt(index);
        RegistroForm registroForm = RegistroForm.getInstance();
        registroForm.setBackForm(this);
        registroForm.setRegistro(registro);
        registroForm.show();
    }

    public void onShow() {
        registrosModel.load();
    }
}
